var up = document.getElementById("up")
var down = document.getElementById("down")
var box = document.getElementById("box")
var form = document.getElementById("form")
// 加载店名
$(document).ready(function() {
    var top = document.getElementById("top")
    var center = document.getElementById("center")
    var name_2 = document.querySelector(".name")
    var goodsid = document.getElementById("goodsid")
    console.log(name_2)
    var id = name_2.innerHTML;
    console.log(id)
    $.ajax({
        type:'get',
        data:{
            userId:id,
        },
        url:"/Merchant/queryForUserShop",
        success:function(data){
            console.log(data);
            console.log(data.t[0].shopName);
            let shopid = data.t[0].shopId;
            console.log(shopid);
            localStorage.setItem('shopid',shopid)
            top.innerHTML +=
                '<h1>' + data.t[0].shopName + '</h1>'
            // 加载店内产品
            $.ajax({
                type:'get',
                data:{
                    shopId:shopid,
                },
                url:"/Merchant/queryForGoods",
                success:function(data){
                    console.log(data);
                    for(i=0;i<data.t.length;i++){
                        center.innerHTML +=
                            `<div class="goods">
                                <input type="text" value="${data.t[i].goodsId}" id="goodsid" style="display: none">              
                                <img src="${data.t[i].goodsImg}" id="image" >
                                <h4 class="name" id="name">${data.t[i].goodsName}</h4>
                                <button id="down" class="${data.t[i].goodId}">下架</button>
                                <button id="newinf" class="${data.t[i].shopId}">更新信息</button>
                            </div>`
                    }
                    var goodsid = document.getElementById("goodsid");
                    var goodsId=goodsid.value;
                    console.log(goodsId)
                    localStorage.setItem('goodsId',goodsId);
                    $(".name").each(function(){
                        var str = $(this).html();
                        var subStr = str.substring(0,10);
                        var data = subStr+(str.length>10?'···':'');
                        $(this).html(data);
                    })
                },
                error:function(){

                }
            })
        },
        error:function(){
             window.location.href="/";
        }
    })
})

// 上架商品
up.onclick = function(){
    box.style.display="none"
    form.style.display="block"
}
var no = document.getElementById("no")
no.onclick = function(){
    box.style.display="block"
    form.style.display="none"
}
var yes = document.getElementById("yes")
//确认上架商品
yes.onclick = function(){
    var brand = document.getElementById("brand");
    var ming = document.getElementById("ming");
    var price = document.getElementById("price");
    var stock = document.getElementById("stock");
    var type = document.getElementById("type");
    var img = document.getElementById("img");
    var store = document.getElementById("store");
    var goodsBrandId = brand.value;
    var goodsName = ming.value;
    var goodsPrice = price.value;
    var goodsStock = stock.value;
    var goodsTypeId = type.value;
    var shopId = store.value;

    var formData = new FormData;
    var file = $('#img')[0].files[0];
    formData.append("goodsBrandId",goodsBrandId);
    formData.append("goodsName",goodsName);
    formData.append("goodsPrice",goodsPrice);
    formData.append("goodsStock",goodsStock);
    formData.append("goodsTypeId",goodsTypeId);
    formData.append("image",file);
    formData.append("shopId",shopId);
    // formData.append('file', $('#img')[0].files[0]); // 固定格式
    // console.log(goodsstock),
    // console.log(formData),
    $.ajax({
        type: "post",
        url: "/Merchant/addGoods",
        data: formData,//上一步的FormData
        cache: false,//不缓存数据
        processData: false,//不转换数据
        contentType: false,
        dataType: "json",
        success: function (data) {
            alert("上架成功");
            window.location.href="/Merchant/personShopCar";
        },
        error:function(){
          alert("上架失败")
    }
    })

}
// 关闭店铺
var close = document.getElementById("close")
close.onclick = function(){
    var id=localStorage.getItem("shopid")
    console.log(id)
    var r=confirm("确定要关闭店铺吗？");
    if (r==true)
    {
        $.ajax({
            type:'get',
            data:{
                shopId:id,
            },
            url:"/Merchant/shutDownShop",
            success:function(data){
                alert("关店成功！");
                window.location.href="/";
                let key=1;
                localStorage.setItem('key',key);
            },
            error:function(){
            //
            }
        })
    }
    else{
        alert("你已取消关店")
    }
}
// 下架产品
var center = document.getElementById("center")
var down = document.getElementById("down")
$('#center').on('click','#down',function(){
    var goodsid=localStorage.getItem("goodsId")
    console.log(goodsid)
    var r=confirm("确定要下架该商品吗？");
    if (r==true)
    {
        $.ajax({
            type:'get',
            data:{
                goodsId:goodsid,
            },
            url:"/Merchant/deleteGoods",
            success:function(data){
                alert("商品已下架")
                window.location.href="/Merchant/personShopCar"
            },
            error:function(){
                //
            }
        })
    }
    else{
        alert("你已取消下架")
    }
})
//更新信息
var newinf = document.getElementById("newinf")
var no = document.getElementById("no")
$('#center').on('click','#newinf',function(){
    // var goodsid=localStorage.getItem("goodsId")
    // console.log(goodsid)
    // $.ajax({
    //     type:"get",
    //     data:{
    //         goodsId:goodsid,
    //     },
    //     url:"/client/findGoodsBygoodsId",
    //     success:function (data) {
    //         console.log(data);
    //         box.style.display="none";
    //         form.style.display="block";
    //         var goodsBrand = data.t[0].goodsBrand;
    //         var goods
    //
    //     },
    //     error:function(){
    //         //
    //     }
       // })
    box.style.display="none"
    form.style.display="block"
    yes.onclick = function(){
        var brand = document.getElementById("brand");
        var ming = document.getElementById("ming");
        var price = document.getElementById("price");
        var stock = document.getElementById("stock");
        var type = document.getElementById("type");
        var img = document.getElementById("img");
        var store = document.getElementById("store");
        var goodsBrandId = brand.value;
        var goodsName = ming.value;
        var goodsPrice = price.value;
        var goodsStock = stock.value;
        var goodsTypeId = type.value;
        var shopId = store.value;

        var formData = new FormData;
        var file = $('#img')[0].files[0];
        formData.append("goodsBrandId",goodsBrandId);
        formData.append("goodsName",goodsName);
        formData.append("goodsPrice",goodsPrice);
        formData.append("goodsStock",goodsStock);
        formData.append("goodsTypeId",goodsTypeId);
        formData.append("image",file);
        formData.append("shopId",shopId);
        // formData.append('file', $('#img')[0].files[0]); // 固定格式
        // console.log(goodsstock),
        // console.log(formData),
        $.ajax({
            type: "post",
            url: "/Merchant/updateGoods",
            data: formData,//上一步的FormData
            cache: false,//不缓存数据
            processData: false,//不转换数据
            contentType: false,
            dataType: "json",
            success: function (data) {
                alert("更新成功");
                window.location.href="/Merchant/personShopCar";
            },
            error:function(){
                alert("更新失败")
            }
        })

    }

})